# -*- coding: utf-8 -*-
"""
Created on Sun Mar 30 20:49:03 2025

@author: Administrator
"""

import os
import pandas as pd

# 设置输入目录路径（请根据实际情况修改）
input_dir = os.path.join("data", "2_missing_value_analysis", "filled", "filledResultDemo")

# 可选择输出到新目录（推荐）或直接覆盖
output_dir = os.path.join("data", "2_missing_value_analysis", "filled", "filledResultDemo_csv")
os.makedirs(output_dir, exist_ok=True)

# 遍历目录中的所有 Excel 文件
for filename in os.listdir(input_dir):
    if filename.endswith(".xlsx"):
        excel_path = os.path.join(input_dir, filename)
        csv_filename = filename.replace(".xlsx", ".csv")
        csv_path = os.path.join(output_dir, csv_filename)

        # 读取 Excel 并保存为 CSV（UTF-8 编码）
        try:
            df = pd.read_excel(excel_path)
            df.to_csv(csv_path, index=False, encoding='utf-8-sig')
            print(f"✅ 转换成功：{filename} → {csv_filename}")
        except Exception as e:
            print(f"❌ 转换失败：{filename}")
            print("错误信息：", e)

print("\\n🎉 所有文件转换完成。")
